<?php

namespace app\admin\controller\recruit;

use app\common\controller\Backend;
use fast\Tree;

/**
 * 会员规则管理
 *
 * @icon fa fa-circle-o
 */
class Recruit extends Backend
{

    /**
     * Ad模型对象
     */
    protected $model = null;
    protected $recruitlist = [];
    protected $searchFields = 'recruit_title';//快速搜索字段
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Recruit');
    }

    /**
     * 查看
     */
    public function index()
    {

        if ($this->request->isAjax())
        {
        //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('key_name'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as &$rec){
                if($rec['step_id']){
                    $rec['step_id']=model('Step')->where('step_id',$rec['step_id'])->value('step_name');
                }
            }
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }
    /**
     * 添加
     */
    public function add()
    {
       return parent::add();
    }
    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $row = $this->model->get($ids);
        return parent::edit($ids);
    }
    public function exam($ids = NULL)
    {
        $row = $this->model->get($ids);
        return parent::edit($ids);
    }
    public function interview($ids = NULL)
    {
        $row = $this->model->get($ids);
        return parent::edit($ids);
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids)
        {
            $delIds = array_unique(explode(',', $ids));
            $count = $this->model->where('recruit_id', 'in', $delIds)->delete();
            if ($count)
            {
                $this->success();
            }
        }
        $this->error();
    }
    /**
     * 下拉搜索
     */
    public function selectpage()
    {
        $this->dataLimit = 'status';
        $this->dataLimitField = 'status';
        return parent::selectpage();
    }
    /**
     * 筛选列
     */
    public function columns()
    {
        $id=$this->request->param("ids");
        $this->assign('rec_id',$id);
        
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = model('Columns')->where($where)->order($sort, $order)->count();
            $column_list = model('Columns')->where($where)->order($sort, $order)->limit($offset, $limit)->select();
            
            $columns_id=$this->model->where('recruit_id',$id)->value('columns_id');
            $columns_id=explode(',',$columns_id);
            foreach($column_list as &$column){
                $column['column_check']=0;//用来判断选中行数据
                if(in_array($column['column_id'],$columns_id)){
                    $column['column_check']=1;
                }
            }
            $result = array("total" => $total, "rows" => $column_list);
            return json($result);
        }
        return $this->view->fetch();
    }
    /**
     * 批量保存
     */
    public function columns_save()
    {
        $ids = $ids ? $ids : $this->request->param("ids");
        $where = $this->request->post("params");
        if ($ids) {
            $data['updatetime']=time();
            $data['columns_id']=$ids;
            $res=$this->model->where($where)->update($data);
            if($res){
                $this->success();
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }
}
